from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils import timezone


class User(AbstractUser):
    """扩展的用户模型"""
    email = models.EmailField(unique=True, verbose_name="邮箱")
    phone = models.CharField(max_length=11, blank=True, null=True, verbose_name="手机号")
    avatar = models.CharField(max_length=500, blank=True, null=True, verbose_name="头像")
    bio = models.TextField(max_length=500, blank=True, verbose_name="个人简介")
    birth_date = models.DateField(blank=True, null=True, verbose_name="生日")
    gender = models.CharField(
        max_length=10,
        choices=[('male', '男'), ('female', '女'), ('other', '其他')],
        blank=True,
        null=True,
        verbose_name="性别"
    )
    
    # 学习相关字段
    learning_level = models.CharField(
        max_length=20,
        choices=[
            ('beginner', '初学者'),
            ('intermediate', '中级'),
            ('advanced', '高级'),
            ('expert', '专家')
        ],
        default='beginner',
        verbose_name="学习水平"
    )
    preferred_language = models.CharField(
        max_length=10,
        choices=[('zh', '中文'), ('en', '英文')],
        default='zh',
        verbose_name="首选语言"
    )
    
    # 系统字段
    is_verified = models.BooleanField(default=False, verbose_name="邮箱验证")
    last_login_ip = models.GenericIPAddressField(blank=True, null=True, verbose_name="最后登录IP")
    created_at = models.DateTimeField(default=timezone.now, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "用户"
        verbose_name_plural = "用户"
        db_table = 'users_user'

    def __str__(self):
        return self.username


class UserProfile(models.Model):
    """用户扩展资料"""
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
    
    # 学习偏好
    learning_goals = models.TextField(blank=True, verbose_name="学习目标")
    interests = models.JSONField(default=list, verbose_name="兴趣领域")
    learning_time_preference = models.CharField(
        max_length=20,
        choices=[
            ('morning', '上午'),
            ('afternoon', '下午'),
            ('evening', '晚上'),
            ('night', '深夜')
        ],
        blank=True,
        null=True,
        verbose_name="学习时间偏好"
    )
    
    # 统计信息
    total_study_time = models.PositiveIntegerField(default=0, verbose_name="总学习时长(分钟)")
    study_streak = models.PositiveIntegerField(default=0, verbose_name="连续学习天数")
    last_study_date = models.DateField(blank=True, null=True, verbose_name="最后学习日期")
    
    # 设置
    notification_enabled = models.BooleanField(default=True, verbose_name="启用通知")
    email_notification = models.BooleanField(default=True, verbose_name="邮件通知")
    study_reminder = models.BooleanField(default=True, verbose_name="学习提醒")
    
    created_at = models.DateTimeField(default=timezone.now, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "用户资料"
        verbose_name_plural = "用户资料"
        db_table = 'users_profile'

    def __str__(self):
        return f"{self.user.username}的资料"